package com.building.hcrm.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.building.hcrm.HcrmApplication.BaiduLocationListener;
import com.building.hcrm.R;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.base.BaseAsyncTask;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.ComFunc;
import com.building.hcrm.common.User;
import com.building.hcrm.model.DoSignResult;
import com.building.hcrm.model.SignParam;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.wcf.CheckInServiceProxy;

public class CheckInActivity extends BaseActivity
{
	private AnalogClock _aClock;
	private DigitalClock _digClock;
	private TextView _txtLocation;
	private LocationClient mLocationClient;
	private Button _btnCheck;
	private Button _btnQuery;
	private ImageButton _imgBtnRe;
	private TextView _txtTitle;
	private ImageButton _imgBtnTitleRight;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_checkin);
		InitTitle();
		_aClock = (AnalogClock) findViewById(R.id.anaClock);
		
		_digClock = (DigitalClock) findViewById(R.id.digClock);
		
		mLocationClient = getHcrmApp().mLocationClient;
		getHcrmApp().setOnBaiduLocationListener(baiduListener);
		_btnCheck = (Button) findViewById(R.id.btnCheck);
		_btnCheck.setOnClickListener(btnCheckOnClick);
		_btnQuery = (Button) findViewById(R.id.btnCheckQuery);
		_btnQuery.setOnClickListener(btnQueryOnClick);
		_txtLocation = (TextView) findViewById(R.id.txtLocation);
		InitLocation();
		mLocationClient.start();
	}

	private void InitTitle()
	{
		_imgBtnRe = (ImageButton) findViewById(R.id.imgbtn_home);
		_imgBtnRe.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// TODO 自动生成的方法存根
				finish();
			}
		});
		_txtTitle = (TextView) findViewById(R.id.titlePageName);
		_txtTitle.setText(R.string.checkin_title);
		_imgBtnTitleRight = (ImageButton) findViewById(R.id.imgbtn_save);
		_imgBtnTitleRight.setVisibility(View.INVISIBLE);
	}

	BaiduLocationListener baiduListener = new BaiduLocationListener()
	{
		@Override
		public void onBaiduLocation(BDLocation location)
		{
			if (null != location)
			{
				_txtLocation.setText(location.getAddrStr());
				_btnCheck.setEnabled(true);
				mLocationClient.stop();
			}
		}
	};

	private class DoSignAsync extends BaseAsyncTask<Object, Object, DoSignResult>
	{
		public DoSignAsync(Context context, String strMsg)
		{
			super(context, strMsg);
		}

		@Override
		protected void onPreExecute()
		{
			super.onPreExecute();
		}

		@Override
		protected DoSignResult doInBackground(Object... arg0)
		{
			DoSignResult obj = null;
			try
			{
				if (null != arg0 && arg0.length > 0)
				{
					obj = CheckInServiceProxy.getInstance(CheckInActivity.this).DoSign((SignParam) arg0[0]);
				}
			}
			catch (Exception e)
			{
				Log.e(LTAG, ComFunc.getExceptionMessage(e), e);
			}
			return obj;
		}

		@Override
		protected void onPostExecute(DoSignResult result)
		{
			super.onPostExecute(result);
			if (null == result)
			{
				Toast.makeText(CheckInActivity.this, R.string.checkin_sign_failed, Toast.LENGTH_SHORT).show();
				return;
			}
			Toast.makeText(CheckInActivity.this,getString( R.string.checkin_sign_success) +ComFunc.GetStringByDate(result.Time, ComConst.DATE_TIME_NOSECOND_FORMAT), Toast.LENGTH_SHORT).show();
		}
	}

	private View.OnClickListener btnCheckOnClick = new View.OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			SignParam param = new SignParam();
			param.userNum = User.getInstance(CheckInActivity.this).getUserNum();
			param.signAddress = _txtLocation.getText().toString();
			new DoSignAsync(CheckInActivity.this,"签到...").execute(param);
		}
	};

	private View.OnClickListener btnQueryOnClick = new View.OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			Intent intent = null;
			intent = new Intent(getApplicationContext(), CheckInListActivity.class);
			startActivity(intent);
		}
	};

	@Override
	protected void onStop()
	{
		// TODO Auto-generated method stub
		mLocationClient.stop();
		super.onStop();
	}

	private void InitLocation()
	{
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度
		option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
		option.setIsNeedAddress(true);
		mLocationClient.setLocOption(option);
	}
}
